﻿using HalconDotNet;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Halcon二次开发.Model;
using Halcon二次开发.Controller.卡尺;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Tools;
using Halcon二次开发.View.多模板匹配;
using Halcon二次开发.View.相机;
using Halcon二次开发.Model.自定义控件;

namespace Halcon二次开发.View.卡尺
{
    public partial class PreciseCaliper : Form
    {
        public string nodeName;
        /// <summary>
        /// 图像变量
        /// </summary>
        HObject RoI_1;
        public Caliper controller;

        HwindowFlowNodeUrseControl hwindowFlowNode;
        public PreciseCaliper(string name, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            InitializeComponent();
            this.nodeName = name;
            this.hwindowFlowNode = hwindowFlowNode; 
            controller = new Caliper($"Data\\工程文件\\{hwindowFlowNode.projectName}\\{hwindowFlowNode.name}任务文件\\精准测量工具卡尺校验数据\\"+ nodeName, this, hwindowFlowNode);
            comboBox1.SelectedIndex = 0;
            //赋值必须要放在后面
            numericUpDown9.Value = 1;
            numericUpDown8.Value = 30;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 1;
            line1Combox.SelectedIndex = 0;
            line2Combox.SelectedIndex = 0;
        }
        /// <summary>
        /// 反射调用的方法，请不要修改
        /// </summary>
        /// <param name="path">Json文件的路径</param>
        /// <param name="addres">节点</param>
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\"+ nodeName+ "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
            controller.LoadStrdb();
        }
        public void H_PMAAlignToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭线与线的显示
            //清除线段显示
            if (controller.line1Object!=null)
            {
                controller.line1Object.ClearDrawingObject();
            }
            if (controller.line2Object != null)
            {
                controller.line2Object.ClearDrawingObject();
            }
            this.Hide();
            e.Cancel = true;
            controller.ClearObject();
        }

        private void H_PMAAlignToolForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 开始抓边
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox2.SelectedItem.Equals("开启"))
                {
                    controller.SetCaliper();
                }
                else {
                    controller.Set_Line();
                }
                
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            hwindowFlowNode.hSmartWindowControl1.SetFullImagePart();
            HOperatorSet.GenEmptyObj(out RoI_1);
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains( "模型匹配"))
                    {

                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }

                if (controller.HideIntputImage == null)
                {
                    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                    return;
                }

                if (comboBox3.Text.ToString()=="开启")
                {
                    controller.DrawLineToLine();
                }
                else if (comboBox2.Text.ToString() == "开启")
                {
                    controller.DrawRecangle(numericUpDown1, numericUpDown2, numericUpDown3, numericUpDown7, numericUpDown10, numericUpDown4, numericUpDown8,
               numericUpDown9, numericUpDown6, numericUpDown5, textBox1, textBox2, textBox3, comboBox1);
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 位置-旋转-长度改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            controller.Move_X(Convert.ToDouble(numericUpDown1.Value), Convert.ToDouble(numericUpDown2.Value),
                Convert.ToDouble(numericUpDown10.Value),Convert.ToDouble(numericUpDown3.Value), Convert.ToDouble(numericUpDown7.Value));
        }

        /// <summary>
        /// 平滑与对比度值修改时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown9_ValueChanged(object sender, EventArgs e)
        {
            controller.Updata_SigamThreshold(double.Parse(numericUpDown9.Value.ToString()), double.Parse(numericUpDown8.Value.ToString()));
        }

        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains("模型匹配"))
                    {

                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }
                controller.Tool_Run();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
            
        }
        /// <summary>
        /// 选取颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Utiy.OpenColorDir(sender,e);
        }

        /// <summary>
        /// 反射调用该方法更新窗体句柄
        /// </summary>
        /// <param name="hwindowFlowNode"></param>
        public void PushHWindon(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.hwindowFlowNode = hwindowFlowNode;
            controller.hwindowFlowNode = hwindowFlowNode;
            controller.path = $"Data\\工程文件\\{hwindowFlowNode.projectName}\\{hwindowFlowNode.name}任务文件\\精准测量工具卡尺校验数据\\" + nodeName;
            controller.hw = hwindowFlowNode.hSmartWindowControl1;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //将这个功能关闭，矩形测量功能
            if (comboBox3.SelectedItem.Equals("开启"))
            {
                comboBox2.SelectedItem = "关闭";
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedItem.Equals("开启"))
            {
                //将线与线的距离测量功能关闭
                comboBox3.SelectedItem = "关闭";
            }
        }

        private void PreciseCaliper_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
    }
}
